package cn.kgcv.tangccp.training;

import org.junit.Test;

/**
 * @author 李昊哲
 * @Description
 * @date 2020/8/17 上午9:12
 */
public class Demo01 {
    @Test
    public void test01() {
        int[] arr = {12, 3, 36};
        // [12,3,36]
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
            if (i < arr.length - 1) {
                System.out.print(",");
            }
        }
        System.out.print("]");
    }

    @Test
    public void test02() {
        int a = 10, b = 20;
        if (a > b) {
            System.out.println(a);
        } else {
            System.out.println(b);
        }
    }

    @Test
    public void test03() {
        int a = 10, b = 20;
        int c = (a > b) ? a : b;
        System.out.println(c);
    }

    @Test
    public void test04() {
        // 31 22 28
        // 22 31 28
        int a = 10, b = 20;
        // c = 10;
        int c = a;
        // a = 20
        a = b;
        //  10
        b = c;
        System.out.println(a);
        System.out.println(b);
    }

    @Test
    public void test05() {
        // 无序数字
        int[] arr = {33, 11, 22};
        // 假定数组中第0号index位置上元素的值为最大值
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            // 遍历数组和参考值max比较只要数组中元素的值大于参考值max则将该元素赋值赐给参考值max
            max = (max < arr[i]) ? arr[i] : max;
        }
        System.out.println(max);
    }

    @Test
    public void test06() {
        // 无序数字
        int[] arr = {33, 11, 22};
        // 假定数组中第0号index位置上元素的值为最大值

        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                int max = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = max;
            }
        }
        System.out.println(arr[arr.length - 1]);
    }

    @Test
    public void test07() {
        // 无序数字
        int[] arr = {33, 11, 22, 20, 18, 38, 16};
        // 假定数组中第0号index位置上元素的值为最大值

        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                int max = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = max;
            }
        }
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
            if (i < arr.length - 1) {
                System.out.print(",");
            }
        }
        System.out.print("]");
    }

    @Test
    public void test08() {
        // 无序数字
        int[] arr = {33, 11, 22, 20, 18, 38, 16};
        // 11,22,20,18,33,16,38
        // 11,20,18,22,16,33,38
        // 11,18,20,16,22,33,38
        // 11,18,20,16,22,33,38
        // 11,18,16,20,22,33,38
        // 11,16,18,20,22,33,38
        // 假定数组中第0号index位置上元素的值为最大值

        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int max = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = max;
                }
            }
        }
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
            if (i < arr.length - 1) {
                System.out.print(",");
            }
        }
        System.out.print("]");
    }

    @Test
    public void test09() {
        int a = 10, b = 20;
        int temp = a;
        a = b;
        b = temp;
        System.out.println(a);
        System.out.println(b);
    }

    @Test
    public void test10() {
        // x + y = z
        // x = z - y
        // y = z - x
        // a = a + b;
        // 30 = 10 + 20;
        // a - b =
        // 30 - 20 = 10

        int a = 10, b = 20;
        // 30 = 10 + 20
        a = a + b;
        // 30 - 20
        // b = 10
        b = a - b;
        // 30 - 10
        // a = 20
        a = a - b;
        System.out.println(a);
        System.out.println(b);
    }

    @Test
    public void test11() {
        int a = 3, b = 5;
        // 00 00 00 11 a 3
        // 00 00 01 01 b 5
        // 00 00 01 10 a 6
        a = a ^ b;
        // 00 00 01 10 a 6
        // 00 00 01 01 b 5
        // 00 00 00 11 b 3
        b = a ^ b;
        // 00 00 01 10 a 6
        // 00 00 00 11 b 3
        // 00 00 01 01 a 5
        a = a ^ b;
        System.out.println(a);
        System.out.println(b);
    }
}
